/*
 * TablePane.java
 *
 * Created on December 11, 2007, 10:40 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fie.contentpanes;

import com.fie.database.Connector;
import com.fie.models.StockTableModel;
import com.fie.renderer.ColorRenderer;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author joseacuna-rohter
 */
public class TablePane {
    
    private StockTableModel stm;
    private JTable table;
    private final Timer timer = new Timer();
    private Connector c;
    private List watchList; 
    private int customerId;
    
    /** Creates a new instance of TablePane */
    public TablePane(int customerId) { 
        this.customerId = customerId;
        start();
    }
    
    

    
    
    public Container createContentPane()
    {
        
        table = new JTable();
        // Table instantiated using the two sets of data.
        stm = new StockTableModel("CME",table);
        
        //stm.start();
        
        table.setModel(stm);     
        table.setBackground(Color.LIGHT_GRAY);
        
        table.setFont(new Font("Arial Terminal",1,12));
        
        
        //Add items to the table(initial loading)
        c = Connector.myConnector;       
        watchList = c.getWatchList(customerId);       
        Iterator it = watchList.iterator();
        while(it.hasNext()){
            addRow(it.next().toString());
       }
        
        
        
        // The table displayed in a Scrollpane.
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(500, 150));
    
        JPanel totalGUI = new JPanel();
        totalGUI.add(scrollPane);
        totalGUI.setOpaque(true);
        return totalGUI;
    }
    
    public void addRow(String ticker){
        stm.addRow(ticker);
        
        
        table.updateUI();
    }
    
    public void removeRow(String ticker){
        stm.removeRow(ticker);
    }
    
    public void updateRow(){
        table.updateUI();
    }
    
    
    public void start() {
        timer.schedule(new TimerTask() {
            public void run() {
                playSound();
                //timer.cancel();
            }
            private void playSound() {
              table.updateUI();
               //System.out.println("IN");
               start();
            }
        }, 2000);
    }
    
    
}
